﻿using System.Windows.Controls;
using Microsoft.Maps.MapControl;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvOpenDistricts.Criteria;
using MyFloridaHouse.libSvOpenDistricts.DataService;
using MyFloridaHouse.libSvOpenDistricts.Dialogs;
using MyFloridaHouse.libSvOpenDistricts.Districts;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;
using MyFloridaHouse.libSvOpenDistricts.SummaryDataGrid;
using MyFloridaHouse.SilverlightOpenDistricts;
using System.Windows;
using System;
using System.Collections.ObjectModel;
using MyFloridaHouse.libSvOpenDistricts.Census;
using MyFloridaHouse.libSvOpenDistricts.MapTools;
using System.Windows.Media;

namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{
    public partial class ViewModelBase
    {

        private Welcome welcomeDlg;


        #region properties

        private bool _isInitializeComplete = false;
        public bool isInitializeComplete
        {
            get { return _isInitializeComplete; }
            set
            {
                if (_isInitializeComplete != value)
                {
                    _isInitializeComplete = value;
                    NotifyPropertyChanged("isInitializeComplete");

                }
            }
        }

        private bool _isLayersInitializeCompleted = false;
        public bool isLayersInitializeCompleted
        {
            get { return _isLayersInitializeCompleted; }
            set
            {
                if (_isLayersInitializeCompleted != value)
                {
                    _isLayersInitializeCompleted = value;
                    NotifyPropertyChanged("isLayersInitializeCompleted");
                }
            }
        }

        private bool _isLayersInitializeError = false;
        public bool isLayersInitializeError
        {
            get { return _isLayersInitializeError; }
            set
            {
                if (_isLayersInitializeError != value)
                {
                    _isLayersInitializeError = value;
                    NotifyPropertyChanged("isLayersInitializeError");
                }
            }
        }
#endregion

        #region major viewModel components
        // set up an interface to our data services
        private ServiceDAL _serviceDAL;
        public ServiceDAL serviceDAL
        {
            get { return _serviceDAL;  }
            set{
                if (_serviceDAL != value) 
                {
                    _serviceDAL = value;
                    NotifyPropertyChanged("serviceDAL"); 
                }
            }
        }

        // Build the pieces of the view model

        public RegionLabel _regionLabel;
        public RegionLabel regionLabel
        {
            get { return _regionLabel; }
            set
            {
                _regionLabel = value;
                NotifyPropertyChanged("regionLabel");
            }
        }

        public DistrictLabel _districtLabel;
        public DistrictLabel districtLabel
        {
            get { return _districtLabel; }
            set
            {
                _districtLabel = value;
                NotifyPropertyChanged("districtLabel");
            }
        }

        public RegionFill _regionFill;
        public RegionFill regionFill
        {
            get { return _regionFill; }
            set
            {
                _regionFill = value;
                NotifyPropertyChanged("regionFill");
            }
        }

        public DistrictFill _districtFill;
        public DistrictFill districtFill
        {
            get { return _districtFill; }
            set
            {
                _districtFill = value;
                NotifyPropertyChanged("districtFill");
            }
        }

        public CriteriaDatasetManager _criteriaDatasetManager;
        public CriteriaDatasetManager criteriaDatasetManager
        {
            get { return _criteriaDatasetManager; }
            set
            {
                _criteriaDatasetManager = value;
                NotifyPropertyChanged("criteriaDatasetManager");
            }
        }

        public SummaryDataGridManager _dataGridManager;
        public SummaryDataGridManager dataGridManager
        {
            get { return _dataGridManager; }
            set
            {
                _dataGridManager = value;
                NotifyPropertyChanged("dataGridManager");
            }
        }

        public DistrictPlanTypes _districtPlanTypes;
        public DistrictPlanTypes districtPlanTypes
        {
            get { return _districtPlanTypes; }
            set
            {
                _districtPlanTypes = value;
                NotifyPropertyChanged("districtPlanTypes");
            }
        }

        public CensusGeometry _censusGeometry;
        public CensusGeometry censusGeometry
        {
            get { return _censusGeometry; }
            set
            {
                _censusGeometry = value;
                NotifyPropertyChanged("censusGeometry");
            }
        }
        #endregion

        public MapLasso mapLasso;

        //TODO: Delete everything associated with FileOpen, note that it propagates to MassDataReport
        //public FileOpen fileOpen;

        


        private StackPanel DisplayPanelForDialogs;

		public void Initialize(Map xamlMap, StackPanel childWindowStackPanel, DataGrid UIDataGridForDistrictSummaries, string relativeBasicHttpServiceAddress)
        {
            DisplayPanelForDialogs = childWindowStackPanel;

            // setup the maps (main and mini)
            myXamlMainMap = xamlMap;

            mapCenter = mapPanControl.mapCenterDefault;
            mapZoomLevel = Constants.MAP_STARTING_ZOOM_LEVEL;
            miniMapZoomLevel = Constants.MINIMAP_STARTING_ZOOM_LEVEL;
            showRoadMap = false;

            // set up the data service 
            serviceDAL = new ServiceDAL(relativeBasicHttpServiceAddress);

            CensusGeometry.CensusGeometryInitialized += new CensusGeometryInitializedEventHandler(CheckInitializedComplete);
            censusGeometry = new CensusGeometry(serviceDAL);

            DistrictPlanTypes.DistrictPlanTypesInitialized += new DistrictPlanTypesInitializedEventHandler(CheckInitializedComplete);
            districtPlanTypes = new DistrictPlanTypes(serviceDAL);
            
            criteriaDatasetManager = new CriteriaDatasetManager(serviceDAL);
            dataGridManager = new SummaryDataGridManager(serviceDAL, UIDataGridForDistrictSummaries);  // depends on LayersDataDictionary to have its data from the DAL
            districtLabel = new DistrictLabel(new RegionLabelAppearance(Colors.Blue, "Arial", Colors.White, 12.0, "Arial", Colors.Yellow, 11.0), dataGridManager);
            districtFill = new DistrictFill();
            SummaryDataGridManager.SummaryDataGridDataChanged += new SummaryDataGridDataChangedEventHandler(districtFill.OnSummaryDataGridDataChanged);

            regionFill = new RegionFill();
            regionLabel = new RegionLabel();

            createDistrictMapLayer();
            districtLabel.RegionLabelDataChanged += new RegionLabelDataChangedEventHandler(OnDistrictLabelDataChanged);
            districtFill.RegionFillConditionsChanged += new RegionFillConditionsChangedEventHandler(OnDistrictFillConditionsChanged);
            districtPlan = new DistrictPlan(districtMapLayer, censusGeometry, districtLabel, districtFill);

            InitializePropertyChangedListener();

            mapLasso = new MapLasso(myXamlMainMap);
            mapLasso.Done += new MapLasso.LassoDoneHandler(OnLassoCompleted);
            mapLasso.Reset(getLassoMode());

            //todo: move these to individual objects like OnServiceDALGetDistrictPlanTypesCompleted
            ServiceDAL.GetLayersCompleted += OnServiceDALGetLayersCompletedEventHandler;
            ServiceDAL.GetRegionDataCompleted += OnServiceDALGetRegionDataCompleted;

            ServiceDAL.GetRegionIDsInViewCompleted += OnServiceDALGetRegionIDsInViewCompleted;
            ServiceDAL.GetRegionListShapesCompleted += OnServiceDALGetRegionListShapesCompleted;
            ServiceDAL.GetRegionListDataCompleted += OnServiceDALGetRegionListDataCompleted;

            // load up the static data
            serviceDAL.GetLayers();


        }



        public void CheckInitializedComplete()
        {
            if ( censusGeometry.isInitializeComplete
                & !censusGeometry.isInitializeError
                & districtPlanTypes.isInitializeComplete
                & !districtPlanTypes.isInitializeError
                & isLayersInitializeCompleted
                & !isLayersInitializeError
                )
            {

                isInitializeComplete = true;
                NotifyPropertyChanged("isInitializeComplete");
                // only show the welcome dialog once at startup after initialization is complete.
                if (welcomeDlg == null)
                {
                    welcomeDlg = new Welcome();
                    welcomeDlg.Show();
                }
            }
        }


    }
}
